// 牛顿迭代法
#include "stdio.h"
#include "math.h"
#define N 100
#define eps 1e-6
#define eta 1e-8

float Newton(float(*f)(float) , float(*f1)(float),float x0)
{
	float x1,d;
	int k = 0;
	do{
		x1 = x0 -(*f)(x0) / (*f1)(x0);
		if (k++ > N || fabs((*f)(x1)) < eps)
		{
			printf("\nNewton die dai fasan!\n");
			break;
		}
		d = fabs(x1) <1? x1-x0:(x1-x0)/x1;
		x0 = x1;
		printf("x(%d)=%f\t",k,x0);
	}
	while(fabs(d)>eps && fabs((*f)(x1)) > eta);
	return x1;
}

float f(float x)
{
	return x*x*x + x*x - 3*x - 3;
}

float f1(float x)
{
	return 3.0*x*x + 2*x - 3;
}

int main( )
{
	float x0,y0;
	printf("Input diedai chuzhi x0: \n");
	scanf("%f",&x0);
	printf("x(0)=%f\n",x0);
	y0 = Newton(f,f1,x0);
	printf("fangcheng de gen :%f\n",y0);
	return 0;
}